package javablackbelt.beans;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SimpleBankService implements BankService {

	protected final static Logger logger = Logger.getLogger(SimpleBankService.class);
	
	private double interestRate;
	
	private Date reportDate;
	
	@Autowired
	private RateExchangeGateway rateExchangeGateway;
	
	@PostConstruct
	public void init() throws Exception {
		logger.info("Executing init()");
	}
	
	@PreDestroy
	public void destroy() throws Exception {
		logger.info("Executing destroy()");
	}
	
	public double computeBalanceWithInterests(double balance) {
		return balance * (1 + getInterestRate()/100.0);
	}

	public String generateReport(CustomerAccount account) {
		StringBuilder reportBuilder = new StringBuilder();
		
		double currentBalance = computeBalanceWithInterests(account.getAverageBalance());
		SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy");
		
		reportBuilder.append("Account Number: " + account.getAccountNumber() + "\n" +
							 "Current Balance: " + currentBalance + " (with interests).\n" +
							 "Account open on: " + sdf.format(account.getOpenDate()));
		return reportBuilder.toString();
	}
	
	public double getInterestRate() {
		return interestRate;
	}

	public void setInterestRate(double interestRate) {
		this.interestRate = interestRate;
	}

	public RateExchangeGateway getRateExchangeGateway() {
		return rateExchangeGateway;
	}

	public void setRateExchangeGateway(RateExchangeGateway rateExchangeGateway) {
		this.rateExchangeGateway = rateExchangeGateway;
	}

	public Date getReportDate() {
		return reportDate;
	}

	public void setReportDate(Date reportDate) {
		this.reportDate = reportDate;
	}

}
